package com.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaobaibai.entity.PayReturn;
import com.xiaobaibai.entity.PaymentRecord;
import com.xiaobaibai.response.ResponseFormat;
import com.xiaobaibai.service.IPayReturnService;
import com.xiaobaibai.service.IPaymentRecordService;
import com.xiaobaibai.vo.PaySelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "支付管理相关接口")
@RequestMapping("/pay")
@Slf4j
@CrossOrigin
public class PayController {

    @Autowired
    private IPaymentRecordService paymentRecordService;//支付记录

    @Autowired
    private IPayReturnService payReturnService;

    @PostMapping("/payList")
    @ApiOperation("获取支付记录集合")
    public ResponseFormat payList(@RequestBody PaySelectVo paySelectVo){
        paySelectVo.setPageNum(paySelectVo.getPageNum()-1);
        List<PaymentRecord> list = paymentRecordService.getList(paySelectVo);
        int count = paymentRecordService.count();
        Map<String,Object> map=new HashMap<>();
        map.put("data",list);
        map.put("total",count);
        return ResponseFormat.ok("ok",map);
    }

    @PostMapping("/returnList")
    @ApiOperation("获取退款记录集合")
    public ResponseFormat returnList(@RequestBody PaySelectVo paySelectVo){
        QueryWrapper<PayReturn> q1=new QueryWrapper<>();
        if(paySelectVo.getStartTime()!=null&&paySelectVo.getEndTime()!=null)
            q1.between("create_time",paySelectVo.getStartTime(),paySelectVo.getEndTime());
        if(StringUtils.isNotBlank(paySelectVo.getOrderId()))
            q1.eq("order_id",paySelectVo.getOrderId());
        IPage<PayReturn> page=new Page<>(paySelectVo.getPageNum(),paySelectVo.getPageSize());
        IPage<PayReturn> pageResult = payReturnService.page(page, q1);
        return ResponseFormat.ok("ok",pageResult);//里面带有页码
    }

    //v3版本的直连商户无退款接口,不做退款的了,让老板娘手动去退款

}
